package cn.tedu.knows.portal.controller;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
    // 定义两个角色常量,讲师和学生,用于Spring-Security框架判断使用
    public static final GrantedAuthority STUDENT = new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER = new SimpleGrantedAuthority("ROLE_TEACHER");

    // 当前控制器方法是登录后访问以决定跳转首页的
    // 所以我们设计它的路径为localhost:8080/和localhost:/8080/index.html
    @GetMapping(value = {"/", "/index.html"})
    public String index(@AuthenticationPrincipal UserDetails user) {
        if (user.getAuthorities().contains(TEACHER)) {
            return "redirect:/index_teacher.html";
        } else if (user.getAuthorities().contains(STUDENT)) {
            return "redirect:/index_student.html";
        }
        return null;
    }
}
